﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Tools
{
    public class MapleHexTool
    {
        public static byte[] GetByteArrayFromHexString(String hex)
        {
            //ByteArrayOutputStream baos = new ByteArrayOutputStream();
            List<byte> Write = new List<byte>();
            int nexti = 0;
            int nextb = 0;

            bool highoc = true;

            while (true)
            {
                int number = -1;
                while (number == -1)
                {
                    if (nexti == hex.Length)
                    {
                        goto outer;
                    }
                    char chr = hex.ToCharArray()[nexti];
                    if ((chr >= '0') && (chr <= '9'))
                    {
                        number = chr - '0';
                    }
                    else if ((chr >= 'a') && (chr <= 'f'))
                    {
                        number = chr - 'a' + 10;
                    }
                    else if ((chr >= 'A') && (chr <= 'F'))
                    {
                        number = chr - 'A' + 10;
                    }
                    else
                    {
                        number = -1;
                    }
                    nexti++;
                }
                if (highoc)
                {
                    nextb = number << 4;
                    highoc = false;
                }
                else
                {
                    nextb |= number;
                    highoc = true;
                    Write.Add((byte)nextb);
                }
            }
        outer:
            return Write.ToArray();
        }


        public static String ToStringFromAscii(byte[] bytes)
        {
            byte[] ret = new byte[bytes.Length];
            for (int x = 0; x < bytes.Length; x++)
            {
                if ((bytes[x] < 32) && (bytes[x] >= 0))
                {
                    ret[x] = 46;
                }
                else
                {
                    ret[x] = bytes[x];
                }
            }
            try
            {
                return Encoding.ASCII.GetString(bytes); //new String(ret, ServerConstants.MAPLE_TYPE.getAscii());
            }
            catch (Exception e)
            {
                return "";
            }
        }


        public static String ToString(byte[] bytes)
        {
            StringBuilder hexed = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                hexed.Append(ToString(bytes[i]));
                hexed.Append(' ');
            }
            return hexed.ToString(0, hexed.Length - 1);
        }

        public static String ToString(byte byteValue)
        {
            int tmp = byteValue << 8;
            char[] retstr = { HEX[(tmp >> 12 & 0xF)], HEX[(tmp >> 8 & 0xF)] };
            string Text = "";
            Text.IndexOfAny(retstr, 0);
            return Text;
        }


        private static char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    }
}
